package com.gamatechno.solodestination;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.gamatechno.adapter.ImageLoader;
import com.gamatechno.solodestination.model.DBHandler;
import com.gamatechno.solodestination.model.Event;



import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.HandlerThread;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

public class EventDetailActivity extends FragmentActivity {
	DBHandler db = new DBHandler(this);
	String uri = "http://sms.gamatechno.com/solo_destination/files/event/";
	String Lat;
	String Lon;
	Bitmap bmp;
	ImageView imageEvent;
	ProgressBar load;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_detail);
		// fvcking actionbar//
		Bitmap b = BitmapFactory.decodeResource(getResources(),
				R.drawable.header_img);
		BitmapDrawable bd = new BitmapDrawable(getResources(), b);
		ActionBar ab = getActionBar();
		ab.setBackgroundDrawable(bd);
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.actionbar_logo, null);
		ab.setCustomView(v);
		ab.setDisplayShowCustomEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setLogo(R.drawable.ic_home);
		getActionBar().setTitle("");
		load = (ProgressBar) findViewById(R.id.load);
		load.bringToFront();
		Bundle extras = getIntent().getExtras();
		String id = extras.getString("id");

		Event e = db.getEventById(id);

		TextView tglEvent = (TextView) findViewById(R.id.tglEvent);
		TextView namaEvent = (TextView) findViewById(R.id.namaEvent);
		ImageView iconMap = (ImageView) findViewById(R.id.iconmap);
		TextView detailEvent = (TextView) findViewById(R.id.detailEvent);
		TextView catEvent = (TextView) findViewById(R.id.categoryEvent);
		imageEvent = (ImageView) findViewById(R.id.imageEvent);
		TextView start = (TextView) findViewById(R.id.startDate);
		TextView end = (TextView) findViewById(R.id.EndDate);

		tglEvent.setText(e.getStartDate());
		namaEvent.setText(e.getName());
		start.setText(e.getStartDate());
		end.setText(e.getEndDate());
		detailEvent.setText(Html.fromHtml(e.getDescription()));
		catEvent.setText(e.getCategory());

		Lat = e.getLat();
		Lon = e.getLon();

		iconMap.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(
		                android.content.Intent.ACTION_VIEW,
		                Uri.parse("http://maps.google.com/maps?daddr="
		                        + Lat + ","
		                        + Lon));
		        startActivity(i);
			}
		});
		Log.d("img", uri + e.getImages());
		if (e.getImages() != null) {
			String imageURL = uri + e.getImages();
			ImageLoader imgLoad = new ImageLoader(this);
			imgLoad.DisplayImage(imageURL, imageEvent);
			if (isNetworkConnected()) {
				new AsyncImage().execute(imageURL);
			}
		} else {
			imageEvent.setImageResource(R.drawable.default_image_big);
		}
	}

	private class AsyncImage extends AsyncTask<String, Void, Boolean> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			load.setVisibility(View.VISIBLE);

		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				URL url = new URL(params[0]);
				Log.d("param", params[0]);
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				connection.setDoInput(true);
				connection.connect();
				InputStream input = connection.getInputStream();
				bmp = BitmapFactory.decodeStream(input);

			} catch (IOException e) {
				e.printStackTrace();

			}
			return true;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			load.setVisibility(View.GONE);
			imageEvent.setImageBitmap(bmp);
		}

	}

	// SEARCH ICON AND CLICKABLE
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);

		// Associate searchable configuration with the SearchView
		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
				.getActionView();
		searchView.setSearchableInfo(searchManager
				.getSearchableInfo(getComponentName()));

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// toggle nav drawer on selecting action bar app icon/title
		// Handle action bar actions click
		switch (item.getItemId()) {
		case R.id.action_search:
			return true;
		case android.R.id.home:
			this.finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/* *
	 * Called when invalidateOptionsMenu() is triggered
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// if nav drawer is opened, hide the action items
		SearchView mSearch = (SearchView) menu.findItem(R.id.action_search)
				.getActionView();
		int searchimg = getResources().getIdentifier(
				"android:id/search_button", null, null);
		ImageView v = (ImageView) mSearch.findViewById(searchimg);
		v.setImageResource(R.drawable.ic_action_search);
		// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		menu.findItem(R.id.action_search).setVisible(true);
		return super.onPrepareOptionsMenu(menu);
	}

	private boolean isNetworkConnected() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni == null) {
			// There are no active networks.
			return false;
		} else
			return true;
	}
}
